home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / test / test17.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  295 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1996. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* Test for GLUT 3.0's overlay functionality. */
  9.  
  10. #ifdef __sgi
  11. #include <malloc.h>
  12. #endif
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <GL/glut.h>
  16. #include <glutint.h>
  17.  
  18. int transP;
  19. int main_win, sub_win;
  20. float x = 0, y = 0;
  21.  
  22. void
  23. render_normal(void)
  24. {
  25.   glutUseLayer(GLUT_NORMAL);
  26.   glClear(GL_COLOR_BUFFER_BIT);
  27.   glColor3f(0.0, 0.0, 1.0);
  28.   glBegin(GL_POLYGON);
  29.   glVertex2f(.2, .28);
  30.   glVertex2f(.5, .58);
  31.   glVertex2f(.2, .58);
  32.   glEnd();
  33. }
  34.  
  35. void
  36. render_overlay(void)
  37. {
  38.   glClear(GL_COLOR_BUFFER_BIT);
  39.   glBegin(GL_POLYGON);
  40.   glVertex2f(.2 + x, .2 + y);
  41.   glVertex2f(.5 + x, .5 + y);
  42.   glVertex2f(.2 + x, .5 + y);
  43.   glEnd();
  44. }
  45.  
  46. void
  47. render(void)
  48. {
  49.   glutUseLayer(GLUT_NORMAL);
  50.   render_normal();
  51.   if (glutLayerGet(GLUT_HAS_OVERLAY)) {
  52.     glutUseLayer(GLUT_OVERLAY);
  53.     render_overlay();
  54.   }
  55. }
  56.  
  57. void
  58. render_sub(void)
  59. {
  60.   printf("render_sub\n");
  61.   glutUseLayer(GLUT_NORMAL);
  62.   render_normal();
  63.   if (glutLayerGet(GLUT_HAS_OVERLAY)) {
  64.     glutUseLayer(GLUT_OVERLAY);
  65.     render_overlay();
  66.   }
  67. }
  68. int display_count = 0;
  69. int damage_expectation;
  70.  
  71. void
  72. timer(int value)
  73. {
  74.   if (value != 777) {
  75.     printf("FAIL: unexpected timer value\n");
  76.     exit(1);
  77.   }
  78.   damage_expectation = 1;
  79.   glutShowWindow();
  80. }
  81.  
  82. void
  83. time2(int value)
  84. {
  85.   if (value == 666) {
  86.     printf("PASS: test17\n");
  87.     exit(0);
  88.   }
  89.   if (value != 888) {
  90.     printf("FAIL: bad value\n");
  91.     exit(1);
  92.   }
  93.   glutDestroyWindow(main_win);
  94.   glutTimerFunc(500, time2, 666);
  95. }
  96.  
  97. void
  98. move_on(void)
  99. {
  100.   display_count++;
  101.   if (display_count == 2) {
  102.     damage_expectation = 1;
  103.     glutIconifyWindow();
  104.     glutTimerFunc(500, timer, 777);
  105.   }
  106.   if (display_count == 4) {
  107.     printf("display_count == 4\n");
  108.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  109.     sub_win = glutCreateSubWindow(main_win, 10, 10, 150, 150);
  110.     glClearColor(0.5, 0.5, 0.5, 0.0);
  111.     glutDisplayFunc(render_sub);
  112.     glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX);
  113.     glutEstablishOverlay();
  114.     glutCopyColormap(main_win);
  115.     glutSetColor((transP + 1) % 2, 0.0, 1.0, 1.0);
  116.     glutRemoveOverlay();
  117.     glutEstablishOverlay();
  118.     glutCopyColormap(main_win);
  119.     glutCopyColormap(main_win);
  120.     glutSetColor((transP + 1) % 2, 1.0, 1.0, 1.0);
  121.     glClearIndex(transP);
  122.     glIndexf((transP + 1) % 2);
  123.     glutSetWindow(main_win);
  124.     glutRemoveOverlay();
  125.     glutTimerFunc(500, time2, 888);
  126.   }
  127. }
  128.  
  129. void
  130. display_normal(void)
  131. {
  132.   if (glutLayerGet(GLUT_NORMAL_DAMAGED) != damage_expectation) {
  133.     printf("FAIL: normal damage not expected\n");
  134.     exit(1);
  135.   }
  136.   render_normal();
  137.   move_on();
  138. }
  139.  
  140. void
  141. display_overlay(void)
  142. {
  143.   if (glutLayerGet(GLUT_OVERLAY_DAMAGED) != damage_expectation) {
  144.     printf("FAIL: overlay damage not expected\n");
  145.     exit(1);
  146.   }
  147.   render_overlay();
  148.   move_on();
  149. }
  150.  
  151. void
  152. display2(void)
  153. {
  154.   static int been_here = 0;
  155.  
  156.   if (glutLayerGet(GLUT_NORMAL_DAMAGED) != 0) {
  157.     printf("FAIL: normal damage not expected\n");
  158.     exit(1);
  159.   }
  160.   if (glutLayerGet(GLUT_OVERLAY_DAMAGED) != 0) {
  161.     printf("FAIL: overlay damage not expected\n");
  162.     exit(1);
  163.   }
  164.   if (been_here) {
  165.     glutPostOverlayRedisplay();
  166.   } else {
  167.     glutOverlayDisplayFunc(display_overlay);
  168.     glutDisplayFunc(display_normal);
  169.     damage_expectation = 0;
  170.     glutPostOverlayRedisplay();
  171.     glutPostRedisplay();
  172.   }
  173. }
  174.  
  175. void
  176. display(void)
  177. {
  178.   if (glutLayerGet(GLUT_NORMAL_DAMAGED) == 0) {
  179.     printf("FAIL: normal damage expected\n");
  180.     exit(1);
  181.   }
  182.   if (glutLayerGet(GLUT_OVERLAY_DAMAGED) == 0) {
  183.     printf("FAIL: overlay damage expected\n");
  184.     exit(1);
  185.   }
  186.   render();
  187.  
  188.   glutDisplayFunc(display2);
  189.   glutPostRedisplay();
  190. }
  191.  
  192. int
  193. main(int argc, char **argv)
  194. {
  195. #if defined(__sgi)  && !defined(REDWOOD)
  196.   /* XXX IRIX 6.0.1 mallopt(M_DEBUG, 1) busted. */
  197.   mallopt(M_DEBUG, 1);
  198. #endif
  199.   glutInit(&argc, argv);
  200.   glutInitWindowSize(300, 300);
  201.   glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX);
  202.  
  203.   if (!glutLayerGet(GLUT_OVERLAY_POSSIBLE)) {
  204.     printf("UNRESOLVED: need overlays for this test\n");
  205.     exit(0);
  206.   }
  207.   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  208.   main_win = glutCreateWindow("test17");
  209.  
  210.   if (glutLayerGet(GLUT_LAYER_IN_USE) == GLUT_OVERLAY) {
  211.     printf("FAIL: overlay should not be in use\n");
  212.     exit(1);
  213.   }
  214.   if (glutLayerGet(GLUT_HAS_OVERLAY)) {
  215.     printf("FAIL: overlay should not exist\n");
  216.     exit(1);
  217.   }
  218.   if (glutLayerGet(GLUT_TRANSPARENT_INDEX) != -1) {
  219.     printf("FAIL: transparent pixel of normal plane should be -1\n");
  220.     exit(1);
  221.   }
  222.   if (glutLayerGet(GLUT_NORMAL_DAMAGED) != 0) {
  223.     printf("FAIL: no normal damage yet\n");
  224.     exit(1);
  225.   }
  226.   if (glutLayerGet(GLUT_OVERLAY_DAMAGED) != -1) {
  227.     printf("FAIL: no overlay damage status yet\n");
  228.     exit(1);
  229.   }
  230.   glClearColor(0.0, 1.0, 0.0, 0.0);
  231.  
  232.   glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX);
  233.  
  234.   /* Small torture test. */
  235.   glutEstablishOverlay();
  236.   glutRemoveOverlay();
  237.   glutEstablishOverlay();
  238.   glutEstablishOverlay();
  239.   glutShowOverlay();
  240.   glutHideOverlay();
  241.   glutShowOverlay();
  242.   glutRemoveOverlay();
  243.   glutRemoveOverlay();
  244.   glutEstablishOverlay();
  245.  
  246.   if (glutGet(GLUT_WINDOW_RGBA)) {
  247.     printf("FAIL: overlay should not be RGBA\n");
  248.     exit(1);
  249.   }
  250.   glutUseLayer(GLUT_NORMAL);
  251.   if (!glutGet(GLUT_WINDOW_RGBA)) {
  252.     printf("FAIL: normal should be RGBA\n");
  253.     exit(1);
  254.   }
  255.   glutUseLayer(GLUT_OVERLAY);
  256.   if (glutGet(GLUT_WINDOW_RGBA)) {
  257.     printf("FAIL: overlay should not be RGBA\n");
  258.     exit(1);
  259.   }
  260.   if (glutLayerGet(GLUT_LAYER_IN_USE) == GLUT_NORMAL) {
  261.     printf("FAIL: overlay should be in use\n");
  262.     exit(1);
  263.   }
  264.   if (glutLayerGet(GLUT_HAS_OVERLAY) == 0) {
  265.     printf("FAIL: overlay should exist\n");
  266.     exit(1);
  267.   }
  268.   if (glutLayerGet(GLUT_TRANSPARENT_INDEX) == -1) {
  269.     printf("FAIL: transparent pixel should exist\n");
  270.     exit(1);
  271.   }
  272.   if (glutLayerGet(GLUT_NORMAL_DAMAGED) != 0) {
  273.     printf("FAIL: no normal damage yet\n");
  274.     exit(1);
  275.   }
  276.   if (glutLayerGet(GLUT_OVERLAY_DAMAGED) != 0) {
  277.     printf("FAIL: no overlay damage yet\n");
  278.     exit(1);
  279.   }
  280.   transP = glutLayerGet(GLUT_TRANSPARENT_INDEX);
  281.   glClearIndex(glutLayerGet(GLUT_TRANSPARENT_INDEX));
  282.   glutSetColor((transP + 1) % 2, 1.0, 0.0, 1.0);
  283.   glIndexi((transP + 1) % 2);
  284.  
  285.   glutUseLayer(GLUT_NORMAL);
  286.   if (glutLayerGet(GLUT_LAYER_IN_USE) == GLUT_OVERLAY) {
  287.     printf("FAIL: overlay should not be in use\n");
  288.     exit(1);
  289.   }
  290.   glutDisplayFunc(display);
  291.  
  292.   glutMainLoop();
  293.   return 0;             /* ANSI C requires main to return int. */
  294. }
  295.